//
//  HomeVideoViewController.m
//  Fireball
//
//  Created by 俞渊华 on 2017/12/25.
//  Copyright © 2017年 俞渊华. All rights reserved.
//

#import "HomeVideoViewController.h"
#import "HomeVideoCell.h"
#import "AticleModel.h"

@interface HomeVideoViewController ()<UITableViewDelegate,UITableViewDataSource>
@property(nonatomic, strong)UITableView *tableView;

@property(nonatomic, strong)NSMutableArray *videoArray;
@property(nonatomic, assign)NSInteger pageSize;
@property(nonatomic, assign)NSInteger pageIndex;
@end

@implementation HomeVideoViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self.view addSubview:self.tableView];
    self.videoArray = [NSMutableArray array];
    
    /** MJRefresh的block也需要用若引用 */
    WEAKSELF
    self.tableView.mj_header = [XMRefreshHeader xm_headerWithRefreshingBlock:^{
        self.pageIndex = 1;
        [self.videoArray removeAllObjects];
        [weakSelf searchVideoAticle];
    }];
    //默认block方法：设置上拉加载更多
    self.tableView.mj_footer = [XMRefreshFooter xm_footerWithRefreshingBlock:^{
        self.pageIndex += 1;
        [weakSelf searchVideoAticle];
    }];
    [self.tableView.mj_header beginRefreshing];
}

-(void)viewDidLayoutSubviews{
    [super viewDidLayoutSubviews];
    self.tableView.frame = CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT - kTabBarHeight - kNav_Height -44);
}


-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    return self.videoArray.count;
    
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return SCREEN_WIDTH * (9.0/16) + 49;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    HomeVideoCell * cell = [HomeVideoCell cellWithTableView:tableView];
    cell.model = self.videoArray[indexPath.row];
    return cell;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
}



-(void)searchVideoAticle{
    NSDictionary * dict = @{@"MenuType":@(self.menu.menuId),
                            @"PageInfo":@{@"CurrentPage":@(self.pageIndex),
                                          @"PageSize":@(self.pageSize)
                                          },
                            @"StrKey":@"",
                            @"iType":@"0"
                            };
    [FBNetworkTool get:@"articleapi/com.SearchAticle" parameters:dict showHUD:NO responseCache:^(XMHttpResponseModel *responseModel) {
        NSArray * array = [AticleModel mj_objectArrayWithKeyValuesArray:responseModel.data];
        [self.videoArray addObjectsFromArray:array];
        [self.tableView reloadData];
        
    } success:^(XMHttpResponseModel *responseModel) {
        [self.tableView.mj_header endRefreshing];
        [self.tableView.mj_footer endRefreshing];
        
        NSArray * array = [AticleModel mj_objectArrayWithKeyValuesArray:responseModel.data];
        [self.videoArray addObjectsFromArray:array];
        if (array.count == 0) {
            [self.tableView.mj_footer endRefreshingWithNoMoreData];
        }
        
        [self.tableView reloadData];
        
    } failure:^(NSError *error) {
        [self.tableView.mj_header endRefreshing];
        [self.tableView.mj_footer endRefreshing];
        
    }];
}
#pragma mark -


- (UITableView *)tableView{
    if (!_tableView) {
        UITableView *tableView = [[UITableView alloc]initWithFrame:CGRectZero style:UITableViewStylePlain];
        tableView.delegate = self;
        tableView.dataSource = self;
        tableView.tableFooterView = [UIView new];
        _tableView = tableView;
    }
    return _tableView;
}

@end
